<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/19
 * Time: 17:44
 */

namespace Admin\Controller;

/**
 * Class EmailController 发送邮箱,发件箱，收件箱
 * @package Admin\Controller
 */
class EmailController extends BaseController
{

	public function sendEail()
	{
		if (IS_POST) {
			$model = D('Email');
			$post = I('post.');
//			dump($post);die;
			$result = $model->saveData($_FILES['file'], $post);

			if ($result) {
				$this->success('发送成功', U('sendBox'));
			} else {
				$this->error('发送失败');
			}

		} else {
//			获取除了用户本人的所有用户
			$model = D('User');
			$result = $model->field('id,truename')->where('id!=' . session('userid'))->select();
//			dump($result); die;
			$this->assign('result', $result);
			$this->display('send');
		}
	}

	public function sendBox()
	{
//select t1.*,t2.truename as truename from sp_email
// as t1 left join sp_user as t2 on t1.to_id = t2.id where t1.from_id = 当前用户的id;
		$model = D('Email');
		$result = $model->field('t1.*,t2.truename as truename')->alias('t1')->join('left join sp_user as t2 on t1.to_id = t2.id')->where('t1.from_id = ' . session('userid'))->select();
//		dump($result);
//		die();
		$this->assign('result', $result);
		$this->display();
	}

	public function receiveBox()
	{
		$model = D('Email');
		$result = $model->field('t1.*,t2.truename as truename')->alias('t1')->join('left join sp_user as t2 on t1.from_id = t2.id')->where('t1.to_id = ' . session('userid'))->select();
//		dump($result);
//		die();
		$this->assign('result', $result);
		$this->display('recBox');
	}

	public function showContent()
	{
		$model = D('Email');
		$get = I('get.id');

		$post['isread'] = '1';
		$post['id'] = $get;

		//		点击的时候并且更新已读
		$model->save($post);
		$result = $model->find($get);
		echo $result['content'];
//		dump($result);die;
	}

	public function delete()
	{
		$model = D('Email');
		$get = I('get.id');
		$result = $model->delete($get);
		if ($result) {
			$this->success('删除成功', U('sendBox'));
		} else {
			$this->error('删除失败');
		}
	}

	public function getCount()
	{
		$model = D('Email');
		$count = $model->where("isread = 0 and to_id = " . session('userid'))->count();
		echo $count;
	}
}